﻿#ifndef ErrLog_h__2014_3_14__14_42
#define ErrLog_h__2014_3_14__14_42

bool df::WriteLog(const df::CC & msg, bool backTrace, const df::CC & file/*=logFile_*/)
{
	df::FileNoLog fi;


	String str;
	str << df::GetDirCurrent() << file;


	if (!fi.Open(str, true, false, false))
	{
		COUT(str << tt_("-------日志文件打开失败(ErrorId:")
			<< df::GetErrorString() << tt_(")-------\n") << msg);
		return false;
	}

	auto fileSize = fi.GetFileSize();


	if (fileSize > df::MAX_LOG_SIZE){
		fi.SetFileSize(0);
		fileSize = 0;
	}

	if (fileSize > 0)
	{
		if (!fi.SeekEnd(0))
		{
			COUT(str << tt_("-------日志SeekEnd失败(ErrorId:")
				<< df::GetErrorString() << tt_(")-------\n") << msg);
			return false;
		}
	}



	str.clear();
	str << tcc_("----- ");
	Time().Now().ToString(str);

	str << tcc_(" -----\r\n");
	if (backTrace)
		df::Err::BackTrace(str);
	str << msg << tcc_("\r\n\r\n");
	if (!fi.Write(str))
	{
		COUT(file << tt_("-------日志文件写入失败(ErrorId:")
			<< df::GetErrorString() << tt_(")-------\n") << msg);
		return false;
	}
	return true;
}


bool df::LastErr(const df::CC & msg, const df::CC & file/*=ErrLog::logFile_*/)
{
#if LAST_ERR_OUT==4
	return true;
#endif

	uint32_t dw = df::GetError();

	String str;

	df::GetErrorString(dw, str);

	str << msg << tcc_("\r\n");
	df::Err::BackTrace(str);

#if LAST_ERR_OUT==0
	return WriteLog(str,false, file);
#elif LAST_ERR_OUT==1
	Cout << file << tt_("------------------\n") << str << std::endl;
	return true;
#elif LAST_ERR_OUT==2
	df::msg(str);
	return true;
#elif LAST_ERR_OUT==3
	throw WinException(std::move(str), dw);
#endif
}

#endif // ErrLog_h__2014_3_14__14_42
